package pfc;

import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import util.DialogoProgreso;
import util.Guardando;
import util.Hilo_espera;
import util.Hilo_guardar;

/**
 *
 * @author <Mejico, Rocío; Mignola, Natalia; Ordoñez, Agustina>
 */
public class PantallaPrincipal extends javax.swing.JFrame {

    private Project prj;
    private GestorProyecto gestorP;
    private JPanel[] panelesPestanias;
    private GestorERS variable;
    private Timer timer;
    private Util util;
    private String nombreArchi;

    /** Creates new form PantallaPrincipal */
    public PantallaPrincipal() throws IOException {
        initComponents();
        this.posicionTamañoPantalla();
        this.setLocationRelativeTo(null);
        gestorP = new GestorProyecto();
        setPanelPestanias();
        itemGuardarProyecto.setEnabled(false);
        itemGuardarComo.setEnabled(false);
        itemImprimir.setEnabled(false);
        validar.setEnabled(false);
        modificarInfoProy.setEnabled(false);

        PrepararSalir();
    }

    public void setPrj(Project prj) {
        this.prj = prj;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        infProyecto = new javax.swing.JPanel();
        etiqLider = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        campoNombreProy = new javax.swing.JTextField();
        campoIDProy = new javax.swing.JTextField();
        campoLiderProy = new javax.swing.JTextField();
        campoFechaCreacionProy = new javax.swing.JTextField();
        campoFechaUActuaProy = new javax.swing.JTextField();
        panelERS = new javax.swing.JTabbedPane();
        panelIntroduccion = new javax.swing.JScrollPane();
        panelDescripcion = new javax.swing.JScrollPane();
        panelRequerimientos = new javax.swing.JScrollPane();
        panelApendInd = new javax.swing.JScrollPane();
        panelLELs = new javax.swing.JDesktopPane();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuBar1 = new javax.swing.JMenuBar();
        archivo = new javax.swing.JMenu();
        itemNuevoProyecto = new javax.swing.JMenuItem();
        itemAbrirProyecto = new javax.swing.JMenuItem();
        itemGuardarProyecto = new javax.swing.JMenuItem();
        itemGuardarComo = new javax.swing.JMenuItem();
        itemImprimir = new javax.swing.JMenuItem();
        itemSalir = new javax.swing.JMenuItem();
        modificar = new javax.swing.JMenu();
        validar = new javax.swing.JMenuItem();
        modificarInfoProy = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OBFRE");

        infProyecto.setBackground(new java.awt.Color(204, 204, 204));
        infProyecto.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(new java.awt.Color(102, 102, 102), null), "Información del Proyecto", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12), new java.awt.Color(0, 0, 0))); // NOI18N

        etiqLider.setText("Lider: ");

        jLabel1.setText("Nombre Proyecto:");

        jLabel2.setText("ID - Proyecto:");

        jLabel3.setText("Fecha Creación: ");

        jLabel4.setText("Fecha Última Actualización:");

        campoNombreProy.setEditable(false);

        campoIDProy.setEditable(false);

        campoLiderProy.setEditable(false);

        campoFechaCreacionProy.setEditable(false);

        campoFechaUActuaProy.setEditable(false);

        javax.swing.GroupLayout infProyectoLayout = new javax.swing.GroupLayout(infProyecto);
        infProyecto.setLayout(infProyectoLayout);
        infProyectoLayout.setHorizontalGroup(
            infProyectoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infProyectoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(infProyectoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(infProyectoLayout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoFechaCreacionProy, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(80, 80, 80)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoFechaUActuaProy, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, infProyectoLayout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(campoNombreProy, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, 138, Short.MAX_VALUE)
                        .addComponent(campoIDProy, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(etiqLider)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campoLiderProy, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))))
        );
        infProyectoLayout.setVerticalGroup(
            infProyectoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(infProyectoLayout.createSequentialGroup()
                .addGroup(infProyectoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoNombreProy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoLiderProy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(campoIDProy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(etiqLider))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(infProyectoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(campoFechaUActuaProy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoFechaCreacionProy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        panelERS.addTab("Introducción", panelIntroduccion);
        panelERS.addTab("Descripción General", panelDescripcion);
        panelERS.addTab("Especificación de Requerimientos", panelRequerimientos);
        panelERS.addTab("Índice y Apendice", panelApendInd);

        panelLELs.setBackground(new java.awt.Color(236, 233, 216));
        panelLELs.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jMenuBar1.setPreferredSize(new java.awt.Dimension(80, 30));

        archivo.setMnemonic('A');
        archivo.setText("Archivo");

        itemNuevoProyecto.setMnemonic('N');
        itemNuevoProyecto.setText("Nuevo Proyecto");
        itemNuevoProyecto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemNuevoProyectoActionPerformed(evt);
            }
        });
        archivo.add(itemNuevoProyecto);

        itemAbrirProyecto.setMnemonic('B');
        itemAbrirProyecto.setText("Abrir Proyecto");
        itemAbrirProyecto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemAbrirProyectoActionPerformed(evt);
            }
        });
        archivo.add(itemAbrirProyecto);

        itemGuardarProyecto.setMnemonic('G');
        itemGuardarProyecto.setText("Guardar ");
        itemGuardarProyecto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemGuardarProyectoActionPerformed(evt);
            }
        });
        archivo.add(itemGuardarProyecto);

        itemGuardarComo.setText("Guardar como");
        itemGuardarComo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemGuardarComoActionPerformed(evt);
            }
        });
        archivo.add(itemGuardarComo);

        itemImprimir.setMnemonic('I');
        itemImprimir.setText("Imprimir");
        itemImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemImprimirActionPerformed(evt);
            }
        });
        archivo.add(itemImprimir);

        itemSalir.setMnemonic('S');
        itemSalir.setText("Salir");
        itemSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemSalirActionPerformed(evt);
            }
        });
        archivo.add(itemSalir);

        jMenuBar1.add(archivo);

        modificar.setMnemonic('H');
        modificar.setText("Herramientas");

        validar.setMnemonic('C');
        validar.setText("Chequear consistencia");
        validar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                validarActionPerformed(evt);
            }
        });
        modificar.add(validar);

        modificarInfoProy.setMnemonic('M');
        modificarInfoProy.setText("Modificar Información de Proyecto");
        modificarInfoProy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                modificarInfoProyActionPerformed(evt);
            }
        });
        modificar.add(modificarInfoProy);

        jMenuBar1.add(modificar);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panelLELs, javax.swing.GroupLayout.PREFERRED_SIZE, 401, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(panelERS, javax.swing.GroupLayout.DEFAULT_SIZE, 911, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(infProyecto, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelERS, javax.swing.GroupLayout.PREFERRED_SIZE, 580, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator1)
                        .addComponent(panelLELs)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(infProyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(6, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jSeparator1, panelERS, panelLELs});

        panelERS.getAccessibleContext().setAccessibleName("Introduccion\n");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1365)/2, (screenSize.height-752)/2, 1365, 752);
    }// </editor-fold>//GEN-END:initComponents

    private void posicionTamañoPantalla() {
        Toolkit tk = Toolkit.getDefaultToolkit();
        int xSize = ((int) tk.getScreenSize().getWidth());
        int ySize = ((int) tk.getScreenSize().getHeight());
        this.setSize(xSize, ySize);
        this.setVisible(true);
        this.setExtendedState(Frame.MAXIMIZED_BOTH);
    }

    //Acciones de los items del menu
    private void itemAbrirProyectoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemAbrirProyectoActionPerformed
        if (prj != null) {
            int confirmado = JOptionPane.showConfirmDialog(this, "¿Desea guardar los cambios en el proyecto actual?", "", JOptionPane.YES_NO_OPTION);
            if (JOptionPane.OK_OPTION == confirmado) {
                gestorP.guardarProyecto(prj);
            }else if(JOptionPane.CLOSED_OPTION == confirmado){
                return;
            }
        }

        util = new Util();
        this.repaint();
        abriendoProyectoExistente();
        if (prj != null) {
            nombreArchi.replaceAll("%20", "_");
            String ruta = util.replaceString(nombreArchi);
            ponerTituloFrame("OBFRE " + ruta);
            this.repaint();
        }

    }//GEN-LAST:event_itemAbrirProyectoActionPerformed

    private void itemNuevoProyectoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemNuevoProyectoActionPerformed
        if (prj != null) {
            int confirmado = JOptionPane.showConfirmDialog(this, "¿Desea guardar los cambios en el proyecto actual?", "", JOptionPane.YES_NO_OPTION);
            if (JOptionPane.OK_OPTION == confirmado) {
                gestorP.guardarProyecto(prj);
            }else if(JOptionPane.CLOSED_OPTION == confirmado){
                return;
            }
        }
        this.repaint();
        this.limpiarTodo();
        this.limpiarTodoInfo();
        this.setTitle("OBFRE");
        this.cargando(0, null);
    }//GEN-LAST:event_itemNuevoProyectoActionPerformed

    private void itemGuardarProyectoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemGuardarProyectoActionPerformed
        this.guardar(0);
        if (prj.getName().equals("OntoSRS_OntoLEL_OntoReq_vacia")) {
            ponerTituloFrame("OBFRE");
        } else {
            ponerTituloFrame("OBFRE " + prj.getLoadingURI().toString().replaceAll("%20", "_").replaceAll("file:/", " "));
        }
        this.actualizarPanelInformacion(prj);
        this.repaint();
    }//GEN-LAST:event_itemGuardarProyectoActionPerformed

    private void itemImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemImprimirActionPerformed
        gestorP.imprimirPDF(variable);
    }//GEN-LAST:event_itemImprimirActionPerformed

    private void itemSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemSalirActionPerformed
        cerrar();
    }//GEN-LAST:event_itemSalirActionPerformed

    private void validarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_validarActionPerformed
        if (prj == null) {
            javax.swing.JOptionPane.showMessageDialog(null, "Debe seleccionar un proyeto para validar");
        } else {
            if (prj.getName().equals("OntoSRS_OntoLEL_OntoReq_vacia")) {
                javax.swing.JOptionPane.showMessageDialog(null, "Debe guardar el proyeto antes de validar ");
            } else {
                this.repaint();
                guardar(0);
                CheckConsistencyAction consistencia = new CheckConsistencyAction();
                OWLModel owlModel = (OWLModel) prj.getKnowledgeBase();
                consistencia.run(owlModel);
            }
        }
    }//GEN-LAST:event_validarActionPerformed

    private void itemGuardarComoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemGuardarComoActionPerformed
        this.guardar(1);
    }//GEN-LAST:event_itemGuardarComoActionPerformed

    private void modificarInfoProyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modificarInfoProyActionPerformed
        this.gestorP.infoProyecto(prj);
        this.actualizarPanelInformacion(prj);
    }//GEN-LAST:event_modificarInfoProyActionPerformed

    private void abriendoProyectoExistente() {
        String nombreArchivo = gestorP.abrirProyecto();
        if (nombreArchivo == null) {
            return;
        }
        this.cargando(1, nombreArchivo);
    }

    private void agregarPanelLELs() {
        panelLELs.removeAll();
        PantallaLEL ventana = new PantallaLEL(prj);
        
        panelLELs.add(ventana);
        ventana.setVisible(true);
    }

    private void cargarPanel(GestorERS variable) throws Excepciones {
        cargarPanelPestaniaIntroduccion(variable, panelesPestanias[0]);
        cargarPanelPestaniaDescripcion(variable, panelesPestanias[1]);
        cargarPanelRequerimientos(variable, panelesPestanias[2]);
        cargarPanelIndiceYApendices(variable, panelesPestanias[3]);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new PantallaPrincipal().setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(PantallaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu archivo;
    private javax.swing.JTextField campoFechaCreacionProy;
    private javax.swing.JTextField campoFechaUActuaProy;
    private javax.swing.JTextField campoIDProy;
    private javax.swing.JTextField campoLiderProy;
    private javax.swing.JTextField campoNombreProy;
    private javax.swing.JLabel etiqLider;
    private javax.swing.JPanel infProyecto;
    private javax.swing.JMenuItem itemAbrirProyecto;
    private javax.swing.JMenuItem itemGuardarComo;
    private javax.swing.JMenuItem itemGuardarProyecto;
    private javax.swing.JMenuItem itemImprimir;
    private javax.swing.JMenuItem itemNuevoProyecto;
    private javax.swing.JMenuItem itemSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JMenu modificar;
    private javax.swing.JMenuItem modificarInfoProy;
    private javax.swing.JScrollPane panelApendInd;
    private javax.swing.JScrollPane panelDescripcion;
    private javax.swing.JTabbedPane panelERS;
    private javax.swing.JScrollPane panelIntroduccion;
    private javax.swing.JDesktopPane panelLELs;
    private javax.swing.JScrollPane panelRequerimientos;
    private javax.swing.JMenuItem validar;
    // End of variables declaration//GEN-END:variables

    /*Contenido de panel de descripción general*/
    private void cargarPanelPestaniaIntroduccion(GestorERS variable, JPanel panelIntro) throws Excepciones {
        JComponent intro = variable.ontoERS("Introduction");
        JComponent purpose = variable.ontoERS("Purpose");
        JComponent scope = variable.ontoERS("Scope");
        JComponent definition = variable.ontoERS("DefinitionAcronymAndAbbreviation");
        JComponent references = variable.ontoERS("Reference");
        JComponent overview = variable.ontoERS("Overview");
        panelIntro.add(intro);
        intro.setVisible(true);
        panelIntro.add(new JLabel(" "));
        panelIntro.add(purpose);
        purpose.setVisible(true);
        panelIntro.add(new JLabel(" "));
        panelIntro.add(scope);
        scope.setVisible(true);
        panelIntro.add(new JLabel(" "));
        panelIntro.add(definition);
        definition.setVisible(true);
        panelIntro.add(new JLabel(" "));
        panelIntro.add(references);
        references.setVisible(true);
        panelIntro.add(new JLabel(" "));
        panelIntro.add(overview);
        overview.setVisible(true);
        panelIntroduccion.setViewportView(panelIntro);
    }
    /*Contenido de panel de descripción general*/

    private void cargarPanelPestaniaDescripcion(GestorERS variable, JPanel panelDesc) throws Excepciones {
        JComponent overallDes = variable.ontoERS("OverallDescription");
        JComponent perspective = variable.ontoERS("ProductPerspective");
        JComponent functions = variable.ontoERS("ProductFunction");
        JComponent educationalLevelUser = variable.ontoERS("EducationalLevelUser");
        JComponent experienceUser = variable.ontoERS("ExperienceUser");
        JComponent technicalExpertiseUser = variable.ontoERS("TechnicalExpertiseUser");
        JComponent auditFunction = variable.ontoERS("AuditFunction");
        JComponent controlFunction = variable.ontoERS("ControlFunction");
        JComponent criticalityOfTheApplication = variable.ontoERS("CriticalityOfTheApplication");
        JComponent hardwareLimitation = variable.ontoERS("HardwareLimitation");
        JComponent higherOrderLanguageRequirement = variable.ontoERS("HigherOrderLanguageRequirement");
        JComponent interfateToOtherApplication = variable.ontoERS("InterfateToOtherApplication");
        JComponent parallelOperation = variable.ontoERS("ParallelOperation");
        JComponent regulatoryPolicy = variable.ontoERS("RegulatoryPolicy");
        JComponent reliabilityRequirement = variable.ontoERS("ReliabilityRequirement");
        JComponent safetyAndSecurityConsideration = variable.ontoERS("SafetyAndSecurityConsideration");
        JComponent signalHandShakeProtocol = variable.ontoERS("SignalHandShakeProtocol");
        JComponent asunYDep = variable.ontoERS("AssumptionAndDependency");
        panelDesc.add(overallDes);
        overallDes.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(perspective);
        perspective.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(functions);
        functions.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(new JLabel(" "));
        panelDesc.add(new JLabel("      2.3 - CARACTERISTICAS DEL USUARIO"));
        panelDesc.add(new JLabel(" "));
        panelDesc.add(educationalLevelUser);
        educationalLevelUser.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(experienceUser);
        experienceUser.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(technicalExpertiseUser);
        technicalExpertiseUser.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(new JLabel(" "));
        panelDesc.add(new JLabel("      2.4 - RESTRICCIONES"));
        panelDesc.add(new JLabel(" "));
        panelDesc.add(auditFunction);
        auditFunction.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(controlFunction);
        controlFunction.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(criticalityOfTheApplication);
        criticalityOfTheApplication.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(hardwareLimitation);
        hardwareLimitation.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(higherOrderLanguageRequirement);
        higherOrderLanguageRequirement.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(interfateToOtherApplication);
        interfateToOtherApplication.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(parallelOperation);
        parallelOperation.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(regulatoryPolicy);
        regulatoryPolicy.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(reliabilityRequirement);
        reliabilityRequirement.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(safetyAndSecurityConsideration);
        safetyAndSecurityConsideration.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(signalHandShakeProtocol);
        signalHandShakeProtocol.setVisible(true);
        panelDesc.add(new JLabel(" "));
        panelDesc.add(asunYDep);
        asunYDep.setVisible(true);
        panelDescripcion.setViewportView(panelDesc);


    }

    /*Contenido de panel de requerimientos*/
    private void cargarPanelRequerimientos(GestorERS variable, JPanel panelReq) throws Excepciones {
        JComponent aplicAsoc = variable.ontoReq("AssociatedApplications");
        JComponent ambFisEsp = variable.ontoReq("PhysicalEnvironmetExpected");
        JComponent estandares = variable.ontoReq("StandardsFullfilment");
        JComponent ambTecEsp = variable.ontoReq("TechnologicalEnvironmentExpected");
        JComponent interCom = variable.ontoReq("CommunicationInterfaces");
        JComponent interHW = variable.ontoReq("HardwareInterfaces");
        JComponent interSW = variable.ontoReq("SoftwareInterfaces");
        JComponent interUser = variable.ontoReq("UserInterfaces");
        JComponent reqFun = variable.ontoReq("FunctionalRequirements");
        JComponent reqApar = variable.ontoReq("AppearanceRequirements");
        JComponent reqHered = variable.ontoReq("LegacyRequirements");
        JComponent reqPolCul = variable.ontoReq("PoliticalCulturalRequirements");
        JComponent reqUso = variable.ontoReq("UseRequirements");
        JComponent precision = variable.ontoReq("AccuracyRequirements");
        JComponent capacidad = variable.ontoReq("CapacityRequirements");
        JComponent critSeg = variable.ontoReq("CriticalSecurityRequirements");
        JComponent velocidad = variable.ontoReq("SpeedRequirements");
        JComponent disponibilidad = variable.ontoReq("Availability");
        JComponent portabilidad = variable.ontoReq("Portability");
        JComponent confiabilidad = variable.ontoReq("Reliability");
        JComponent confidencialidad = variable.ontoReq("Conficenciality");
        JComponent soporte = variable.ontoReq("Support");
        JComponent infoConfidencial = variable.ontoReq("ConfidentialInformation");
        JComponent interEDI = variable.ontoReq("EDIExchange");
        JComponent adaptabilidad = variable.ontoReq("Adaptability");
        JComponent construccion = variable.ontoReq("Construction");
        JComponent durabilidad = variable.ontoReq("Durability");
        JComponent condAmb = variable.ontoReq("EnvironmentalConditions");
        JComponent polReg = variable.ontoReq("PolicyAndRegulation");
        JComponent slcs = variable.ontoReq("SystemLifeCycleSustainment");
        JComponent personalSopComp = variable.ontoReq("ComputingSupportStaff");
        JComponent personalEmerg = variable.ontoReq("EmergenciesStaff");
        JComponent personalInvitado = variable.ontoReq("GuestStaff");
        JComponent personalSGC = variable.ontoReq("ManagementControlServicesStaff");
        JComponent personalGestion = variable.ontoReq("ManagementStaff");
        JComponent personalOper = variable.ontoReq("OperationStaff");
        JComponent personalSeg = variable.ontoReq("SecurityStaff");
        JComponent personalSop = variable.ontoReq("SupportingStaff");
        JComponent confSistema = variable.ontoReq("SystemReliability");
        JComponent sopSistema = variable.ontoReq("SystemSupport");
        JComponent caractPerSist = variable.ontoReq("SystemPerformanceCharacteristics");
        JComponent segSist = variable.ontoReq("SystemSecurity");
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.1 - REQUERIMIENTOS DE SOFTWARE"));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.1.1 - Restricciones de diseño"));
        panelReq.add(new JLabel(" "));
        panelReq.add(aplicAsoc);
        aplicAsoc.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(ambFisEsp);
        ambFisEsp.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(estandares);
        estandares.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(ambTecEsp);
        ambTecEsp.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("     3.1.2 - Requerimientos de la interface externa"));
        panelReq.add(new JLabel(" "));
        panelReq.add(interCom);
        interCom.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(interHW);
        interHW.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(interSW);
        interSW.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(interUser);
        interUser.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(reqFun);
        reqFun.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("     3.1.4 - Otros requerimientos"));
        panelReq.add(new JLabel(" "));
        panelReq.add(reqApar);
        reqApar.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(reqHered);
        reqHered.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(reqPolCul);
        reqPolCul.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(reqUso);
        reqUso.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("     3.1.5 - Requerimientos de Performance"));
        panelReq.add(new JLabel(" "));
        panelReq.add(precision);
        precision.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(capacidad);
        capacidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(critSeg);
        critSeg.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(velocidad);
        velocidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("     3.1.6 - Atributos del Sistema de Software"));
        panelReq.add(new JLabel(" "));
        panelReq.add(disponibilidad);
        disponibilidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(portabilidad);
        portabilidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(confiabilidad);
        confiabilidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.1.6.4 - Seguridad"));
        panelReq.add(new JLabel(" "));
        panelReq.add(confidencialidad);
        confidencialidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(soporte);
        soporte.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.2 - REQUERIMIENTOS DEL SISTEMA"));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.2.1 - Gestión de la información"));
        panelReq.add(new JLabel(" "));
        panelReq.add(infoConfidencial);
        infoConfidencial.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(interEDI);
        interEDI.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.2.2 - Físicos"));
        panelReq.add(new JLabel(" "));
        panelReq.add(adaptabilidad);
        adaptabilidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(construccion);
        construccion.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(durabilidad);
        durabilidad.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(condAmb);
        condAmb.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(polReg);
        polReg.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(slcs);
        slcs.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.2.5 - Operaciones del sistema"));
        panelReq.add(new JLabel(" "));
        panelReq.add(new JLabel("      3.2.5.1 - Factores humanos del sistema"));
        panelReq.add(new JLabel(" "));
        panelReq.add(personalSopComp);
        personalSopComp.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalEmerg);
        personalEmerg.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalInvitado);
        personalInvitado.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalSGC);
        personalSGC.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalGestion);
        personalGestion.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalOper);
        personalOper.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalSeg);
        personalSeg.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(personalSop);
        personalSop.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(confSistema);
        confSistema.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(sopSistema);
        sopSistema.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(caractPerSist);
        caractPerSist.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelReq.add(segSist);
        segSist.setVisible(true);
        panelReq.add(new JLabel(" "));
        panelRequerimientos.setViewportView(panelReq);

    }

    /*Contenido de panel de índice y apéndice*/
    private void cargarPanelIndiceYApendices(GestorERS variable, JPanel panelIndiceYApendice) throws Excepciones {
        JComponent index = variable.ontoERS("Index");
        JComponent appendixes = variable.ontoERS("Appendix");
        panelIndiceYApendice.add(index);
        index.setVisible(true);
        panelIndiceYApendice.add(appendixes);
        appendixes.setVisible(true);
        panelApendInd.setViewportView(panelIndiceYApendice);

    }

    private void cerrar() {
        if (prj == null) {
            System.exit(0);
        } else if (prj.getName().equals("OntoSRS_OntoLEL_OntoReq_vacia")) {
            int confirmado = JOptionPane.showConfirmDialog(this, "¿Desea guardar el proyecto?", "", JOptionPane.YES_NO_OPTION);
            if (JOptionPane.OK_OPTION == confirmado) {
                guardar(1);
                System.exit(0);
            } else if (JOptionPane.NO_OPTION == confirmado) {
                System.exit(0);
            } else if (JOptionPane.CLOSED_OPTION == confirmado) {
                this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE );
            }
        } else {
            int confirmado = JOptionPane.showConfirmDialog(this, "¿Desea guardar los cambios en " + prj.getName() + "?", "", JOptionPane.YES_NO_OPTION);
            if (JOptionPane.OK_OPTION == confirmado) {
                guardar(0);
                System.exit(0);
            } else if (JOptionPane.NO_OPTION == confirmado) {
                System.exit(0);
            } else if (JOptionPane.CLOSED_OPTION == confirmado) {
               this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE );
            }
        }
      
    }

    private void PrepararSalir() {
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent evt) {
                cerrar();
              }
        });

    }

    private void cargando(int i, String nombreA) {
        final DialogoProgreso dialogoProgreso = new DialogoProgreso(this, "progreso");
        dialogoProgreso.pack();
        dialogoProgreso.setLocationRelativeTo(this);
        dialogoProgreso.setVisible(true);

        final Hilo_espera worker;

        if (i == 0) {
            worker = new Hilo_espera(this);
        } else {
            worker = new Hilo_espera(this, nombreA);
            cargarNombreArchivo(nombreA);
        }
        worker.execute();
        timer = new Timer(0, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (worker.isDone()) {
                    try {
                        setPanelPestanias();
                        variable = new GestorERS(prj);
                        cargarPanel(variable);
                        agregarPanelLELs();
                        limpiarTodoInfo();
                        actualizarPanelInformacion(prj);
                        itemGuardarProyecto.setEnabled(true);
                        itemGuardarComo.setEnabled(true);
                        itemImprimir.setEnabled(true);
                        validar.setEnabled(true);
                        modificarInfoProy.setEnabled(true);
                        dialogoProgreso.dispose();
                        terminar();
                    } catch (Excepciones ex) {
                        dialogoProgreso.dispose();
                        terminar();
                        limpiarTodo();
                        JOptionPane.showMessageDialog(null, ex.getMessage());
                    }
                }
            }
        });
        timer.start();
    }

    private void guardar(int i) {
        final Guardando dialogoGuardar = new Guardando(this, "Guardando");
        dialogoGuardar.pack();
        dialogoGuardar.setLocationRelativeTo(this);
        dialogoGuardar.setVisible(true);

        final Hilo_guardar worker;
        if (i == 0) {
            worker = new Hilo_guardar(prj, 0);
        } else {
            worker = new Hilo_guardar(prj, 1);
        }
        worker.execute();
        timer = new Timer(0, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (worker.isDone()) {
                    if (prj.getName().equals("OntoSRS_OntoLEL_OntoReq_vacia")) {
                        ponerTituloFrame("OBFRE");
                    } else {
                        ponerTituloFrame("OBFRE " + prj.getLoadingURI().toString().replaceAll("%20", "_").replaceAll("file:/", " "));
                    }
                    actualizarPanelInformacion(prj);
                    repaint();
                    dialogoGuardar.dispose();
                    terminar();
                }
            }
        });
        timer.start();
    }

    private void cargarNombreArchivo(String p) {
        nombreArchi = p;
    }

    private void terminar() {
        timer.stop();
    }

    private void actualizarPanelInformacion(Project prj) {
        String[] info = this.gestorP.getInformacion(prj);
        for (int i = 0; i < info.length; i++) {
            if (info[i].length() >= 47) {
                info[i] = info[i].substring(0, 47);
            }
        }
        this.campoNombreProy.setText(info[0]);
        this.campoIDProy.setText(info[1]);
        this.campoLiderProy.setText(info[2]);
        this.campoFechaCreacionProy.setText(info[3]);
        this.campoFechaUActuaProy.setText(info[4]);
    }

    private void limpiarTodoInfo() {
        this.campoNombreProy.setText("");
        this.campoIDProy.setText("");
        this.campoLiderProy.setText("");
        this.campoFechaCreacionProy.setText("");
        this.campoFechaUActuaProy.setText("");
    }

    private void ponerTituloFrame(String titulo) {
        this.setTitle(titulo);
    }

    public void limpiarTodo() {
        for (int i = 0; i < panelesPestanias.length; i++) {
            panelesPestanias[i].removeAll();
        }
        this.panelLELs.removeAll();
        this.panelLELs.repaint();
        this.limpiarTodoInfo();
        this.repaint();
        ponerTituloFrame("OBFRE");
        prj = null;
        itemGuardarProyecto.setEnabled(false);
        itemGuardarComo.setEnabled(false);
        itemImprimir.setEnabled(false);
        validar.setEnabled(false);
        modificarInfoProy.setEnabled(false);
    }

    private void setPanelPestanias() {
        panelesPestanias = new JPanel[4];//introduccion[0], descripción general[1], requerimientos[2], indice y apendices[3]

        for (int j = 0; j < 4; j++) {
            panelesPestanias[j] = new JPanel();
        }
        for (int i = 0; i < panelesPestanias.length; i++) {
            panelesPestanias[i].setLayout(new BoxLayout(panelesPestanias[i], BoxLayout.Y_AXIS));
        }
    }
}
